package com.competition.reception.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.commonutils.JwtUtils;
import com.competition.commonutils.R;
import com.competition.reception.entity.*;
import com.competition.reception.entity.vo.CompetitionQueryVo;
import com.competition.reception.entity.vo.ExaminationInfo;
import com.competition.reception.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识竞赛测试表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@RestController
@RequestMapping("/reception/examination")
@Api(description = "竞赛相关的接口")
@CrossOrigin
public class ExaminationController {


    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private UserCompetitionService userCompetitionService;

    @Autowired
    private UserAdditionalService userAdditionalService;

    @Autowired
    private ExaminationDetailService examinationDetailService;

    @ApiOperation("竞赛高级查询加分页")
    @PostMapping("getExaminationList")
    public R getExaminationList(@RequestBody CompetitionQueryVo queryVo){
        System.out.println(queryVo);
        List<ExaminationInfo> examinationInfoList=examinationService.getExaminationInfoList(queryVo);
        int examinationCount=examinationService.getExaminationInfoCount(queryVo);
        return R.ok().data("examinationCount",examinationCount).data("examinationInfoList",examinationInfoList);
    }

    @PostMapping("addExamination")
    public R addExamination(@RequestBody Examination examination, HttpServletRequest request){
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        examination.setManageId(memberIdByJwtToken);
        System.out.println(examination);
        examinationService.save(examination);
        return R.ok();
    }

    @ApiOperation("根据竞赛id删除竞赛")
    @DeleteMapping("deleteExamination/{id}")
    public R deleteExamination(@PathVariable String id){
        //删除竞赛
        examinationService.removeById(id);
        //删除竞赛对应的标题
        Map<String,Object> deleteExaminationDetailMap=new HashMap<>();
        deleteExaminationDetailMap.put("examination_id",id);
        examinationDetailService.removeByMap(deleteExaminationDetailMap);
        //删除用户参与的竞赛
        //1.查询要删除的数据
        //Map<Object,Object> userMap=new HashMap<>();
        QueryWrapper<UserCompetition> userCompetitionQueryWrapper=new QueryWrapper<>();
        userCompetitionQueryWrapper.eq("examination_id",id);
        List<UserCompetition> userCompetitionList = userCompetitionService.list(userCompetitionQueryWrapper);
        //2.删除
        Map<String,Object> deleteUserCompetition=new HashMap<>();
        deleteExaminationDetailMap.put("examination_id",id);
        userCompetitionService.removeByMap(deleteExaminationDetailMap);
        //更改用户总分
        for(int i=0;i<userCompetitionList.size();i++){
            String examinationId = userCompetitionList.get(i).getExaminationId();
            String userId = userCompetitionList.get(i).getUserId();
            Integer score = userCompetitionList.get(i).getScore();
            QueryWrapper<UserAdditional> userAdditionalQueryWrapper=new QueryWrapper<>();
            userAdditionalQueryWrapper.eq("user_id",userId);
            UserAdditional userAdditional = userAdditionalService.getOne(userAdditionalQueryWrapper);
            Integer totalScore = userAdditional.getTotalScore();
            totalScore=totalScore-score;
            userAdditional.setTotalScore(totalScore);
            UpdateWrapper<UserAdditional> userAdditionalUpdateWrapper=new UpdateWrapper<>();
            userAdditionalUpdateWrapper.eq("user_id",userId);
            userAdditionalService.update(userAdditional,userAdditionalUpdateWrapper);
        }
        return R.ok();
    }

    @ApiOperation("竞赛结果统计")
    @GetMapping("getExaminationInfo/{id}")
    public R getExaminationInfo(@PathVariable String id){
        QueryWrapper<UserCompetition> userCompetitionQueryWrapper=new QueryWrapper<>();
        userCompetitionQueryWrapper.eq("examination_id",id);
        List<UserCompetition> userCompetitionList = userCompetitionService.list(userCompetitionQueryWrapper);
        return R.ok().data("userCompetitionList",userCompetitionList);
    }
}

